package com.chzc.fm.duotin.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chzc.fm.duotin.R;
import com.chzc.fm.duotin.bean.mine.SpecialBean;
import com.chzc.fm.duotin.db.AlbumDao;
import com.chzc.fm.duotin.utils.DuotinTask;
import com.chzc.fm.duotin.utils.FileUtils;
import com.chzc.fm.duotin.utils.HttpUtil;
import com.chzc.fm.duotin.utils.ImageUtil;
import com.chzc.fm.duotin.utils.LogUtil;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.io.File;
import java.util.List;

/**
 * Created by BillSinla on 2016/7/3.
 */
public class SpecialPanel extends LinearLayout implements View.OnClickListener, SpecialItem.OnItemUpdateListener{
    private RelativeLayout rlAlbum;
    private ImageView imageAvatar;
    private TextView textTitle, textUpdate;
    private LinearLayout llContent;
    private SpecialItem specialItem1, specialItem2;
    private View horizontalLine;

    private AlbumDao albumDao;

    private SpecialBean specialBean;
    public SpecialPanel(Context context, SpecialBean specialBean) {
        super(context);
        init(context);
        initEvent();
        setSpecialBean(specialBean);
    }

    public SpecialPanel(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        inflate(context, R.layout.widget_special_panel, this);

        albumDao = new AlbumDao(context);

        rlAlbum = (RelativeLayout) findViewById(R.id.rl_special_album);
        imageAvatar = (ImageView) findViewById(R.id.iv_special_avatar);
        textTitle = (TextView) findViewById(R.id.tv_special_album);
        textUpdate = (TextView) findViewById(R.id.tv_special_num);
        llContent = (LinearLayout) findViewById(R.id.ll_special_content);
        specialItem1 = (SpecialItem) findViewById(R.id.si_special_item1);
        specialItem2 = (SpecialItem) findViewById(R.id.si_special_item2);
        horizontalLine = findViewById(R.id.v_special_line);
    }

    private void initEvent() {
        rlAlbum.setOnClickListener(this);
        specialItem1.setOnClickListener(this);
        specialItem2.setOnClickListener(this);
        specialItem1.setOnItemUpdateListener(this);
        specialItem2.setOnItemUpdateListener(this);
    }

    public void setSpecialBean(final SpecialBean specialBean) {
        this.specialBean = specialBean;
        SpecialBean.AlbumBean album = specialBean.getAlbum();
        List<SpecialBean.ContentListBean> contents = specialBean.getContent_list();
        ImageLoader.getInstance().displayImage(album.getImage_url(), imageAvatar, ImageUtil.getRoundOptions());
        textTitle.setText(album.getTitle());
        textUpdate.setText("节目数量：" + album.getContent_num());

        if(contents != null && contents.size() > 0){
            if(contents.size() == 1){
                horizontalLine.setVisibility(GONE);
                specialItem2.setVisibility(GONE);
                specialItem1.setContent(contents.get(0));
            }else{
                specialItem1.setContent(contents.get(0));
                specialItem2.setContent(contents.get(1));
            }
        }else{
            llContent.setVisibility(GONE);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.rl_special_album:
                break;
            case R.id.si_special_item1:
                break;
            case R.id.si_special_item2:
                break;
        }
    }

    //当点击下载时，也将专辑信息存入数据库
    @Override
    public void update() {
        SpecialBean.AlbumBean albumBean = specialBean.getAlbum();
        String id = albumBean.getId();
        List<SpecialBean.AlbumBean> albumBeans = albumDao.getAlbum(id);

        if(albumBeans.size() == 1) {
            albumBean = albumBeans.get(0);

            //更新数量
            int num = Integer.parseInt(albumBean.getContent_num()) + 1;
            albumBean.setContent_num("" + num);

            albumDao.updateAlbum(albumBean);
        }else if(albumBeans.size() == 0){
            albumBean.setContent_num("1");
            albumDao.insertAlbum(albumBean);
//            downloadImage(albumBean.getImage_url());
        }

    }

    //为存入数据库专辑下载图片文件
    private void downloadImage(final String url) {
        File file = new File(FileUtils.DIR_IMAGE, FileUtils.getFileNameByHashCode(url));
        LogUtil.w("SpecialPanel: file_name:" + file.toString());
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpUtil.download(url, FileUtils.DIR_IMAGE, FileUtils.getFileNameByHashCode(url), null);
            }
        });
    }
}
